package controller;

import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import ch.phovoz.tlog.DataTraining;
import ch.phovoz.tlog.InfoObject;
import ch.phovoz.tlog.database.DatabaseManager;

public class DatabaseController{
	private DatabaseManager dbmanager;
	private SQLiteDatabase tdatabase;
	private Activity acitvity;
	private InfoObject infoobject;
	private Date datenow;
	
	
	public DatabaseController(Activity activity){
		this.acitvity=activity;
		dbmanager= new DatabaseManager(this.acitvity);
		infoobject=InfoObject.getInfoObject();
	  	
	}
	
	public String getExersice(int pos){
		tdatabase=dbmanager.getReadableDatabase();
		 Cursor tCursor=
				 
				   tdatabase.rawQuery("Select name from "+DatabaseManager.TABLE_EXERSICENAME +
				" where _id ='" + pos+ "'", null 
					   					  
					   );
		 		
		tCursor.moveToLast();
		tdatabase.close();
		return tCursor.getString(0);
	}
	
	public void StartTraining(){
		datenow=new Date();
		tdatabase=dbmanager.getWritableDatabase();
		tdatabase.execSQL("INSERT INTO "+DatabaseManager.TABLE_Training
 				+"(startdate,enddate,total) "+
 				"VALUES ('"+datenow.toString()+"'," +
 						"'"+""+"',"+
 						"'0'"+
 						");"	   
 					  
 					   );

 			   Cursor tCursor=
 					   tdatabase.rawQuery("Select * from "+
 					   DatabaseManager.TABLE_Training +";",
 					    null);
 			tCursor.moveToLast();
 			InfoObject.setCurrentTID(tCursor.getInt(0));
 			Toast.makeText(acitvity,((Integer) InfoObject.getCurrentTID()).toString(), Toast.LENGTH_LONG).show();
 			tdatabase.close();
 			   
	}
	
	public void StartExersice(){
		datenow=new Date();
		tdatabase=dbmanager.getWritableDatabase();
		
		tdatabase.execSQL("INSERT INTO "+DatabaseManager.TABLE_EXERSICE
				+"(trainingsid,starttime,endtime,totalw,exersiceNameid) "+
				"VALUES ('"+InfoObject.getCurrentTID()+"'," +
						"'"+datenow.toString()+"',"+
						"'"+""+"',"+
						"'0',"+
						"'"+InfoObject.getCurrentExersice()+"'"+
						");"	   
					  
					   );
		Cursor tCursor=
				   tdatabase.rawQuery("Select * from "+
						  DatabaseManager.TABLE_EXERSICE +";",
						    null);

		   tCursor.moveToLast();
		   InfoObject.setCurrentExersiceDBID(tCursor.getInt(0));
		   Toast.makeText(acitvity,((Integer) InfoObject.getCurrentExersiceDBID()).toString(), Toast.LENGTH_LONG).show();
		   tdatabase.close();
	}
	public void addSet(String weights, String reps){
		tdatabase=dbmanager.getWritableDatabase();
		tdatabase.execSQL("INSERT INTO "+DatabaseManager.TABLE_REPSET
				+"(exID,weight,reps)"+
				"VALUES ('"+InfoObject.getCurrentExersiceDBID()+"'," +
						"'"+weights+"',"+
						"'"+reps+"'"+
						");"	   
					  
					   );
		
		Toast.makeText(acitvity,weights+" " + "reps" + reps, Toast.LENGTH_LONG).show();
		tdatabase.close();
	}

}
